home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
CUGUK
/
PROG_TOO
/
C013.ZIP
/
MV.C
< prev
next >
Wrap
Text File
|
1990-01-19
|
3KB
|
114 lines
/********************************************************************
* C Users Group (U.K) C Source Code Library File CUGLIB.013 *
* Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd. *
* Edgbaston, Birmingham B15 2QN ENGLAND *
********************************************************************
* File name: mv.c
* Program name:mv
* Source of file: Ron Wellstead
* Purpose: An MS-DOS copy of the UNIX utility of the same name.
* Changes: <who what when & why major changes have been made>
********************************************************************/
/*
*
* @(#) mv.c 1.2 87/07/27
*
* UNIX style mv utility for dos
*
* copyright (c) 1987 Ron Wellsted.
* This software is provided on the understanding that it is
* NOT to be used for commercial gain. It may be freely distributed
* in source or object form among amateur and hobby computer users ONLY!
*
* renames or moves files or directories.
* usage: mv file1 file2
* or: mv file1 file2 ... directory
* or: mv directory1 directory2
* written for Microsoft C, link with setargv.obj to expand wildcards
*/
#include <io.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
char *basename();
char buffer[128]="@(#)mv.c VR 1.0.0 15 Jul 1987";
struct stat file_stat;
main(argc,argv)
int argc;
char *argv[];
{
int i;
if (argc<3)
{
fprintf(stderr,"usage: mv file1 .... fileN|directory\n");
exit(1);
}
if ((stat(argv[argc-1],&file_stat)==0)&&(file_stat.st_mode&S_IFDIR))
{
for (i=1;i<argc-1;i++)
{
strcpy(buffer,argv[argc-1]);
strcat(buffer,"\\");
strcat(buffer,basename(argv[i]));
if (movefile(argv[i],buffer)) exit(1);
}
}
else
{
if (argc>3)
{
fprintf(stderr,"mv: %s not directory\n",argv[argc-1]);
exit (1);
}
exit(movefile(argv[1],argv[2]));
}
}
movefile(file1,file2)
char *file1,*file2;
{
if (access(file1,0)) /* file 1 exists ? */
{
fprintf(stderr,"mv: ");
perror(file1);
return 1;
}
if (access(file2,0)==0) /* file 2 exists ? */
{
if (access(file2,2)) /* writable ? */
{
fprintf(stderr,"mv: %s exists, can't remove\n",file2);
return 1;
}
if (unlink(file2))
{
fprintf(stderr,"mv: ");
perror(file2);
return 1;
}
}
if (rename(file1,file2))
{
perror("mv");
return 1;
}
return 0;
}
char *basename(str)
char *str;
{
char *ptr;
for (ptr=str;*ptr!='\0';ptr++)
{
if ((*ptr=='\\')||(*ptr=='/')) str=ptr+1;
}
return str;
}